<?php

namespace App\Http\Controllers\Admin\Users;

use App\Http\Controllers\Controller;
use App\Http\Requests\UserVerify;
use App\Services\Admin\Users\UsersService;

class UserController extends Controller
{
    public function index (){
        $data=request()->all();
        return response()->json([
            'code' => 200,
            'msg'=>'ok',
            'data' => [
                'token'=>'admin-token'
            ]
        ]);
    }

    public function info (){
        return response()->json([
            'code' => 200,
            'data' => [
                'roles' => [
                    'admin'
                ],
                'name' => 'rongfei',
                'avatar' => 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                'introduction' => 'rf'
            ]
        ]);
    }

    public function logout (){
        return response()->json([
            'code' => 200,
            'data' => 'success'
        ]);
    }

    public function userList (){
        try {
            $search_part['key'] = request()->get('key', '');
            $search_part['value'] = request()->get('info', '');
            $search_other['type'] = request()->get('type', '');
            $search_other['grade'] = request()->get('grade', '');
            $search_other['group'] = request()->get('group', '');
            $search_other['vip'] = request()->get('vip', '');
            $search_other['sexs'] = request()->get('sexs', '');
            $search_other['expand_status'] = request()->get('status', '');
            $search_visit['state'] = request()->get('visit', '');
            $search_tag = request()->get('tag', '');
            $search_order_num = request()->get('num', '');
            $page = request()->get('currentPage', 1);
            $limit['size'] = request()->get('pagesize', 10);
            $limit['offset'] = ($page - 1) * 10;
            if (!empty($search_visit['state'])){
                $search_visit['date'] = request()->get('time');
            }
            $data = UsersService::userList($search_part,$search_other,$search_tag,$search_visit,$search_order_num,$limit);
            return self::success(200,'查询成功',$data);
        } catch (\Exception $exception){
            return self::success(2001,'查询失败');
        }
    }

    public function userAdd (UserVerify $verify){
        try {
            $data = request()->all();
            $res = UsersService::userAdd($data);
            return self::success(200,'添加完成');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function couponList (){
        try {
            $page = request()->get('currentPage', 1);
            $search = request()->get('search', '');
            $offset = ($page - 1) * 3;
            $res = UsersService::couponList($offset,$search);
            return self::success(200,'优惠券列表获取成功',$res);
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function sendCoupon (){
        try {
            $userid = request()->post('userid');
            $coupon = request()->post('coupon');
            $res = UsersService::sendCoupon($userid,$coupon);
            return self::success(200,'优惠券发送成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }

    public function sendVip (){
        try {
            $userid = request()->post('id');
            $num = request()->post('num');
            $res = UsersService::sendVip($userid,$num);
            return self::success(200,'会员赠送成功');
        } catch (\Exception $exception){
            return self::success(2001,$exception->getMessage());
        }
    }
}
